package Domain;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;


import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 
 * Makes a receipt for the buyer in PDF format.
 * Will show information about the book and its cost.
 * 
 * @author Rodrigo Acuna
 * 
 */


public class PrintReceipt {
	private  String FILE = "/Users/rodrigo/Desktop/";
	//private  String FILE2 = "/Users/rodrigo/Desktop/" + (get) + ".pdf";
//	private static String FILE = "C:/Users/Leinad/workspace/bokhandelpdf/kvitto.pdf";
	private  Font catFont = new Font(Font.FontFamily.HELVETICA, 18,
			Font.BOLD);
	private  Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
			Font.BOLD);
	private  Font smallBold = new Font(Font.FontFamily.HELVETICA, 12,
			Font.BOLD);
	private Book selectedBook;
	
	public void setReceiptData() {
		try {
			Document receipt = new Document();
			PdfWriter.getInstance(receipt, new FileOutputStream(FILE));
			receipt.open();
			addContent(receipt);
			System.out.println("PDF kvitto klar!");
			receipt.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public PrintReceipt(Book b) {
		selectedBook = b;
		FILE = FILE + b.getTitle() + "_" + createSqlDateFormat(new Date()) + ".pdf";
		setReceiptData();
		
	}
	
	private  void addContent(Document receipt) throws DocumentException {
		Anchor anchor = new Anchor("Kontantkvitto:", catFont);
		anchor.setName("Kontantkvitto:");

		// Second parameter is the number of the chapter
		Chapter catPart = new Chapter(new Paragraph(anchor), 1);
		
		// Add a table
		headerTable(catPart);

//		addEmptyLine(7 2);

		Paragraph subPara = new Paragraph("Subcategory 2", subFont);
		Section subCatPart = catPart.addSection(subPara);
		subCatPart.add(new Paragraph("SöderS Andrahandsbokhandel"));
		subCatPart.add(new Paragraph("Alfred Nobels Allé 9"));
		subCatPart.add(new Paragraph("14189 Huddinge"));
		

		subCatPart.add(new Paragraph("bokhandel@soders.nu"));
		
		
		// Add a table
		createTable(subCatPart);
		
		// Now add all this to the document
		receipt.add(catPart);
	}

	/**
	 * 
	 * 
	 * @param subCatPart
	 * @throws BadElementException
	 */
	private  void headerTable(Section subCatPart) throws BadElementException{
		PdfPTable table2 = new PdfPTable(2); 
		table2.getDefaultCell().setBorder(PdfPCell.NO_BORDER); 
		table2.setHorizontalAlignment(Element.ALIGN_RIGHT);
		table2.setWidthPercentage(50);

		
		table2.addCell(new Phrase("Datum"));
		table2.addCell(new Phrase("Kvittonr"));
	    table2.addCell(new Paragraph("" + createSqlDateFormat(new Date())));
	    table2.addCell("211");	
	          
	    subCatPart.add(table2);
	}
	
	private void createTable(Section subCatPart)	throws BadElementException {

		float[] widths1 = { 2f, 1f, 1f };
		PdfPTable table = new PdfPTable(widths1);
		table.setWidthPercentage(100);
		table.setSpacingBefore(15f);
		table.getDefaultCell().setPadding(6);
//		table.getDefaultCell().setPaddingTop(6);

		PdfPCell c1 = new PdfPCell(new Phrase("Betalningen avser"));
		c1.setHorizontalAlignment(Element.ALIGN_LEFT);
//		c1.setBackgroundColor(Color.LIGHT_GRAY);
		table.addCell(c1);

		c1 = new PdfPCell(new Phrase("Antal"));
		c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(c1);

		
		c1 = new PdfPCell(new Phrase("Belopp"));
		c1.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(c1);
		
		table.setHeaderRows(1);

		table.addCell(selectedBook.getTitle());
		table.addCell("1");
		table.addCell(String.valueOf(selectedBook.getPrice()));
		table.addCell("Designing Interactive Systems");
		table.addCell("1");
		table.addCell("280");
		table.addCell("Jag är Zlatan");
		table.addCell("1");
		table.addCell("150");
		
		PdfPCell empty = new PdfPCell();
		empty.setBorder(0);
		empty.setColspan(1);
		
		//calculates the total amount
		double summa = (300+280+150);
        table.addCell(empty);
		table.addCell("Summa:");
		table.addCell(String.valueOf(summa));

		table.addCell(empty);
		table.addCell("Varav moms:");
		
		//calculates the taxes
		double moms = (summa*0.06);
		System.out.println(moms);
		table.addCell(String.valueOf(moms));

		table.addCell(empty);
		table.addCell("Att betala:");
		
		double attBetala = summa ;
		table.addCell(String.valueOf(attBetala));
		
		
		subCatPart.add(table);

	}

	private String createSqlDateFormat(Date dateCreated) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		return formatter.format(dateCreated);
	}

	private static void addEmptyLine(Paragraph paragraph, int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}
	
}